package sg.edu.nus.iss.vmcs.system;

import sg.edu.nus.iss.vmcs.store.DrinksBrand;
import sg.edu.nus.iss.vmcs.store.DrinksStoreItem;
import sg.edu.nus.iss.vmcs.store.StoreItem;

/** 
 * @version 1.0
 * @author Low Chen Wei
 */

public class DrinkLoader implements ItemLoader {
	
	private static final String PROP_NUM_ITEMS = "NumOfItems";
	private static final String NAME_LABEL     = "Name";
	private static final String PRICE_LABEL    = "Price";
	private static final String QUANTITY_LABEL = "Quantity";
	
	private PropertyImp propImp;
	
	public DrinkLoader(PropertyImp propImp) {
		this.propImp = propImp;
	}
	
	public StoreItem getItem(Integer identifier) {
		int idx = identifier.intValue() + 1;
		DrinksBrand brand = new DrinksBrand();

		String value = propImp.getValue(String.valueOf(idx), NAME_LABEL).toString();
		brand.setName(value);
		
		value = propImp.getValue(String.valueOf(idx), PRICE_LABEL).toString();
		brand.setPrice(Integer.parseInt(value));

		value = propImp.getValue(String.valueOf(idx), QUANTITY_LABEL).toString();
		int qty = Integer.parseInt(value);

		DrinksStoreItem item = new DrinksStoreItem(brand, qty);
		return item;
	}

	public void setItem(Integer index, StoreItem item) {
		int idx = index + 1;

		DrinksStoreItem drinkItem = (DrinksStoreItem) item;
		DrinksBrand brand = (DrinksBrand) drinkItem.getContent();
		
		propImp.setValue(String.valueOf(idx), NAME_LABEL, brand.getName());
		propImp.setValue(String.valueOf(idx), PRICE_LABEL, brand.getPrice());
		propImp.setValue(String.valueOf(idx), QUANTITY_LABEL, drinkItem.getQuantity());
	}
	
	public Integer getNumOfItems() {
		String nm = propImp.getValue(PROP_NUM_ITEMS).toString();
		int nmi;
		nmi = Integer.parseInt(nm);
		return nmi;
	}
	
	public void setNumOfItems(Integer items) {
		propImp.setValue(PROP_NUM_ITEMS, String.valueOf(items));
	}

}
